package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
@Slf4j
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 分页查询资源列表
     * @param resourceDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("查询资源列表")
    @ApiImplicitParam(name = "resourceDto",value = "列表查询Dto对象",required = false,dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.parentResourceNo","resourceDto.resourceType"})
    private ResponseResult<List<ResourceVo>> list(@RequestBody ResourceDto resourceDto) {

        List<ResourceVo> list =resourceService.list(resourceDto);

        return ResponseResult.success(list);
    }


    /**
     * @param resourceDto 资源对象
     * @return
     *  资源树形
     */
    @PostMapping("/tree")
    @ApiOperation(value = "资源树形", notes = "资源树形")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.level"})
    private ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto) {
        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);
        return ResponseResult.success(treeVo);
    }

    /**
     * 添加资源
     * @param resourceDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    private ResponseResult createResource(@RequestBody ResourceDto resourceDto) {
        resourceService.createResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 启用或者禁用
     * @param resourceDto
     * @return
     */
    @PostMapping("/enable")
    @ApiOperation(value = "资源启用或者禁用")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.parentResourceNo"
            , "resourceDto.resourceNo"})
    private ResponseResult enableResource(@RequestBody ResourceDto resourceDto) {
            Boolean flag = resourceService.enableResource(resourceDto);
        return ResponseResult.success(flag);
    }

    /**
     * 修改资源
     * @param resourceDto
     * @return
     */
    @PostMapping
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto) {
        resourceService.update(resourceDto);
        return ResponseResult.success();
    }

    /**
     * @return
     * 动态菜单
     */
    @GetMapping("/menus")
    @ApiOperation(value = "动态菜单", notes = "动态菜单")
    public ResponseResult<List<MenuVo>> menus() {
        List<MenuVo> list =  resourceService.menus();
        return ResponseResult.success(list);
    }
}
